#include <stdio.h>
#include <memory.h>

int main( )
{
    int n, i, j;
    __int64 a[ 1000 ], d[ 1000 ], maxn;
    while ( scanf("%d", &n) != EOF && n )
    {
        memset( d, 0, sizeof( d ) );
        for ( i = 0; i < n; i++ )
            scanf("%I64d", &a[ i ]);
        maxn = d[ 0 ] = a[ 0 ];
        for ( i = 1; i < n; i++ )
        {
            for ( j = 0; j < i; j++ )
                if ( d[ i ] < d[ j ] && a[ i ] > a[ j ] )
                    d[ i ] = d[ j ];
            d[ i ] += a[ i ];
            if ( maxn < d[ i ] ) maxn = d[ i ];
        }
        printf("%I64d\n", maxn);
    }
    return 0;
}
